package cn.ty.znsz.teacher;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

import cn.ty.znsz.area.AreaService;
import cn.ty.znsz.common.interceptor.PostInterceptor;
import cn.ty.znsz.common.model.TeacherComment;
import cn.ty.znsz.common.model.TeacherInfo;
import cn.ty.znsz.common.utils.CommonUtils;
import cn.ty.znsz.user.UserService;

@Path("/teacher")
public class TeacherController extends Controller{
	
	@Inject 
	UserService us;
	@Inject 
	AreaService as;
	@Inject
	TeacherService ts;
	
	/**
	 * 首页
	 */
	public void index() {
		String cookie=getCookie("jznsz");
		Ret ret=us.getMenuPoint(getParaToInt("id"),cookie);
		Ret ret2=as.getUserArea(cookie);
		setAttr("powers", ret.get("data"));
		setAttr("areas", JsonKit.toJson(ret2.get("data")));
		render("index.html");
	}
	/**
	 * 首页分页
	 */
	public void paginate() {
		Page<TeacherInfo> page=ts.paginate(getParaToInt("pageNumber"), getParaToInt("pageSize"),getPara("name"),getPara("school"),getPara("major"),getPara("isCheck"),
				getPara("city"),getPara("county"),getPara("town"),getPara("village"));
		renderJson(Ret.create().set("msg", "操作成功").set("code", 0).set("count", page.getTotalRow()).set("data", page.getList()));
	}
	/**
	 * 跳转添加页
	 */
	public void teacherAdd() {
		Ret ret=as.getAreasById(getCookie("jznsz"));		
		setAttr("areas", JsonKit.toJson(ret.get("data")));
		render("teacherAdd.html");
	}
	/**
	 * 上传图片
	 */
	public void imgUpload() {
		String sort=new SimpleDateFormat("yyyyMM").format(new Date());
		String dir="/upload/article/images/"+sort;
		List<UploadFile> files=getFiles(dir);
		if(files.size()>0) {
			Ret ret=CommonUtils.imgUpload(files,dir);
			renderJson(ret);
		}else {
			renderJson(Ret.fail("上传失败").set("code", 1));
		}
	}
	/**
	 * 上传视频
	 */
	public void videoUpload() {
		String sort=new SimpleDateFormat("yyyyMM").format(new Date());
		String dir="/upload/article/video/"+sort;
		UploadFile uf=getFile("video",dir);
		File file=uf.getFile();
		Ret ret=CommonUtils.videoUpload(file,dir);
		renderJson(ret);
	}
	/**
	 * 保存师资
	 */
	@Before({PostInterceptor.class,teacherValidator.class})
	public void teacherSave() {
		TeacherInfo data=getModel(TeacherInfo.class, "data");
		Ret ret=ts.teacherSave(data,getParaToInt("areaId"),getPara("contentValue"),getCookie("jznsz"));
		renderJson(ret);
	}
	/**
	 * 跳转编辑页
	 */
	public void teacherEdit() {
		String areaCode=getPara("areaCode");
		int areaId=as.getAreaIdByAreaCode(areaCode);
		Ret ret=as.getAreasById(getCookie("jznsz"));		
		setAttr("areas", JsonKit.toJson(ret.get("data")));
		setAttr("areaId", areaId);
		render("teacherEdit.html");
	}
	/**
	 * 修改师资
	 */
	@Before({PostInterceptor.class,teacherValidator.class})
	public void teacherUpdate() {
		TeacherInfo data=getModel(TeacherInfo.class, "data");
		Ret ret=ts.teacherUpdate(data,getParaToInt("areaId"),getPara("contentValue"),getParaToInt("id"),getCookie("jznsz"));
		renderJson(ret);
	}
	/**
	 * 删除师资
	 */
	@Before(PostInterceptor.class)
	public void teacherDel() {
		Ret ret=ts.teacherDel(getParaToInt("id"));
		renderJson(ret);
	}
	/**
	 * 跳转审核页
	 */
	public void teacherCheck() {
		keepPara("id");
		render("teacherCheck.html");
	}
	/**
	 * 审核师资
	 */
	@Before(PostInterceptor.class)
	public void teacherChecks() {
		Ret ret=ts.teacherChecks(getParaToInt("id"),getPara("isCheck"));
		renderJson(ret);
	}
	/**
	 * 跳转上传页
	 */
	public void teacherUpload() {
		keepPara("id");
		render("teacherUpload.html");
	}
	/**
	 * 上传头像保存
	 */
	@Before(PostInterceptor.class)
	public void teacherUploadImg() {
		String dir="/upload/haibao/"+new SimpleDateFormat("yyyyMM").format(new Date());
		UploadFile uf = getFile("image", dir);
		File file=uf.getFile();
		Ret ret = ts.teacherUploadImg(getParaToInt("id"),file,dir);
		renderJson(ret);
	}
	/**
	 * 跳转评价管理页
	 */
	public void teacherComment() {
		keepPara("id");
		render("teacherComment.html");
	}
	/**
	 * 评价分页
	 */
	public void commentPaginate() {
		Page<TeacherComment> page=ts.commentPaginate(getParaToInt("pageNumber"), getParaToInt("pageSize"),getParaToInt("id"));
		renderJson(Ret.create().set("msg", "操作成功").set("code", 0).set("count", page.getTotalRow()).set("data", page.getList()));
	}
	/**
	 * 跳转添加评论页
	 */
	public void commentAdd() {
		keepPara("id");
		render("commentAdd.html");
	}
	/**
	 * 保存评价
	 */
	@Before(PostInterceptor.class)
	public void commentSave() {
		Ret ret=ts.commentSave(getParaToInt("id"),getPara("commentValue"),getCookie("jznsz"));
		renderJson(ret);
	}
	/**
	 * 删除评价
	 */
	@Before(PostInterceptor.class)
	public void commentDel() {
		Ret ret=ts.commentDel(getParaToInt("id"));
		renderJson(ret);
	}
	/**
	 * 跳转评论审核页
	 */
	public void commentCheck() {
		keepPara("id");
		render("commentCheck.html");
	}
	/**
	 * 审核评价
	 */
	@Before(PostInterceptor.class)
	public void commentChecks() {
		Ret ret=ts.commentChecks(getParaToInt("id"),getPara("isCheck"));
		renderJson(ret);
	}
}
